package com.zqxx.examine.portal.security;


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.zqxx.examine.model.user.User;
import com.zqxx.examine.portal.persistence.UserMapper;

@Component("userDetailsServiceImpl")
public class UserDetailsServiceImpl
  implements UserDetailsService
{
  private UserInfo userInfo;
  @Autowired
  public UserMapper userDao;
  
  public UserDetails loadUserByUsername(String username)
    throws UsernameNotFoundException
  {
    User user = this.userDao.getUserByName(username);
    if (user == null) {
      throw new UsernameNotFoundException("user not found!");
    }
    List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles());
    this.userInfo = new UserInfo(username, user.getPassword(), true, true, true, true, authorities);
    this.userInfo.setUserid(user.getUserId());
    this.userInfo.setRolesName(user.getRoles());
    return this.userInfo;
  }
}
